<template>
  <BasicModal
    v-bind="$attrs"
    v-on="$listeners"
    :title="t('component.excel.exportModalTitle')"
    @ok="handleOk"
    @register="registerModal"
  >
    <BasicForm :labelWidth="100" :schemas="schemas" :showActionButtonGroup="false" @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
import type { ExportModalResult } from './typing'
import { defineComponent } from 'vue'
import { BasicModal, useModalInner } from '/@/vbenComponents/Modal'
import { BasicForm, FormSchema, useForm } from '/@/vbenComponents/Form/index'
import { useMessage } from '/@/hooks/web/useMessage'
import { useI18n } from '/@/hooks/web/useI18n'

const { t } = useI18n()

const schemas: FormSchema[] = [
  {
    field: 'filename',
    component: 'Input',
    colProps: { span: 14 },
    label: t('component.excel.fileName'),
    // rules: [{ required: true }],
  },
  {
    field: 'bookType',
    component: 'Select',
    label: t('component.excel.fileType'),
    defaultValue: 'xlsx',
    // rules: [{ required: true }],
    componentProps: {
      options: [
        {
          label: 'xlsx',
          value: 'xlsx',
          key: 'xlsx',
        },
        {
          label: 'html',
          value: 'html',
          key: 'html',
        },
        {
          label: 'csv',
          value: 'csv',
          key: 'csv',
        },
        {
          label: 'txt',
          value: 'txt',
          key: 'txt',
        },
      ],
    },
  },
]
export default defineComponent({
  components: { BasicModal, BasicForm },
  emits: ['success', 'register'],
  setup(_, { emit }) {
    const [registerForm, { getFieldsValue }] = useForm()
    const [registerModal, { closeModal }] = useModalInner()
    const { createMessage } = useMessage()

    async function handleOk() {
      const res = (await getFieldsValue()) as ExportModalResult
      console.log(res)
      const { filename, bookType } = res
      if (!filename) return createMessage.error('请输入导出的文件名')
      emit('success', {
        filename: `${filename.split('.').shift()}.${bookType}`,
        bookType,
      })
      closeModal()
    }

    return {
      schemas,
      handleOk,
      registerForm,
      registerModal,
      t,
    }
  },
})
</script>
